<?php namespace Home\Controller;
use Common\Model\Goods;
use Common\Model\Goods_attr;
use Common\Model\Type_attr;
use Common\Model\Goods_list;
use Common\Model\Goods_detail;
//内容页控制器
class ContentController extends CommonController{
	public function index(){
		//p($_SESSION);
		$gid=Q('get.gid',0,'intval');
		//根据gid查询当前的商品
		$goodsModel=new Goods;
		$goodsData=$goodsModel->where("gid={$gid}")->find();
		//根据gid查询当前商品的商品属性
		$goods_attrModel=new Goods_attr;
		$goods_attrData=$goods_attrModel
				->join('type_attr','shop_type_attr_taid','=','taid')
				->where("shop_goods_gid={$gid} ANd class=1")->get();
		$temp=[];
		//把相同taid的属性放在一起
		foreach($goods_attrData as $v){
			$temp[$v['shop_type_attr_taid']][]=$v;
		}
		//p($temp);
		//重新组合名字和值
		$fallTamp=[];
		foreach($temp as $taid=>$v){
			$type_attrModel=new Type_attr;
			$name=$type_attrModel->where("taid='{$taid}'")->pluck('taname');
			$fallTamp[]=[
				'name'=>$name,
				'value'=>$v
			];
		}
		View::with('fallTamp',$fallTamp);
		View::with('goodsData',$goodsData);
		//获取商品详情
		$goods_detailModel=new Goods_detail;
		$detailData=$goods_detailModel->where("shop_goods_gid={$gid}")->find();
		$detailData['dimg']=explode('|', $detailData['dimg']);
//		p($detailData);
		View::with('detailData',$detailData);
		//查询本产品的属性
		$attrData=$goods_attrModel
				->join('type_attr','shop_type_attr_taid','=','taid')
				->where("shop_goods_gid={$gid} ANd class=0")->get();
		View::with('attrData',$attrData);
		//显示热销商品
		$oneGoodsData=$goodsModel
			->where("shop_category_cid<>42")
			->orderBy('click','DESC')
			->limit(6)->get();
//		p($oneGoodsData);
		View::with('oneGoodsData',$oneGoodsData);
		View::make();
	}
	public function goodsList(){
	    $combine=Q('post.gaids');
		$combine=substr($combine, 0,strlen($combine)-1);
		$goods_listModel=new Goods_list;
		$goodsList=$goods_listModel->where("combine='{$combine}'")->find();
		$good_attrModel=new Goods_attr;
		if($goodsList){
		$gaadd=$good_attrModel->where("gaid IN (".str_replace('|', ',', $goodsList['combine']).")")->lists('gaadd');
		$add=0;
		foreach($gaadd as $v){
			$add=$v+$add;
		}
		$goodsList['add']=$add;
		}
		echo json_encode($goodsList);
	}
}
?>